package com.sens.database;


import android.content.Context;
import android.os.Environment;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.enums.AssignType;
import com.litesuits.orm.db.model.ConflictAlgorithm;
import com.sens.common.Config;

import java.io.File;
import java.io.Serializable;
import java.util.List;


/**
 * Created by SensYang on 2016/4/8 0008.
 */
public class LiteOrmDBUtil {
    public static class BaseTable<T extends LiteOrmDBUtil.BaseTable> implements Serializable, Cloneable, Comparable<T> {
        // 设置为主键,自增
        @PrimaryKey(AssignType.AUTO_INCREMENT)
        @Column("_pid")
        private int id;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public int compareTo(T o) {
            return o.getId() - this.id;
        }
    }

    private static LiteOrmDBUtil globalDBUtil;
    private LiteOrm liteOrm;
    private String DBName;

    /**
     * 获取全局数据库
     */
    public static LiteOrmDBUtil getGlobalDBUtil(Context context) {
        if (globalDBUtil == null) {
            File databasesPath = new File(Environment.getExternalStorageDirectory(), "OrderBack");
            if (!databasesPath.exists()) {
                databasesPath.mkdirs();
            }
            globalDBUtil = new LiteOrmDBUtil();
            globalDBUtil.DBName = new File(databasesPath, "SensYang").getAbsolutePath();
        }
        globalDBUtil.createDb(context, globalDBUtil.DBName);
        return globalDBUtil;
    }

    private LiteOrmDBUtil() {
    }

    /**
     * 创建数据库
     */
    private void createDb(Context context, String dbName) {
        liteOrm = LiteOrm.newSingleInstance(context, dbName);
        liteOrm.setDebugged(Config.ISDEBUG);
    }

    /**
     * 插入一条记录
     */
    public <T extends BaseTable> void save(T t) {
        if (liteOrm == null || t == null) return;
        liteOrm.save(t);
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 插入所有记录
     */
    public <T extends BaseTable> void save(List<T> list) {
        if (liteOrm == null || list == null) return;
        liteOrm.save(list);
        liteOrm.close();
        liteOrm = null;
    }


    /**
     * 删除一个
     */
    public <T extends BaseTable> void delete(T t) {
        if (liteOrm == null || t == null) return;
        liteOrm.delete(t);
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 删除多个
     */
    public <T extends BaseTable> void delete(List<T> list) {
        if (liteOrm == null || list == null) return;
        liteOrm.delete(list);
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 删除所有 某字段等于 Vlaue的值
     */
    public <T extends BaseTable> void deleteByWhere(Class<T> cla, String field, String... value) {
        if (liteOrm == null || cla == null || value == null) return;
        liteOrm.delete(WhereBuilder.create(cla).where(field + "=?", value));
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 删除所有 小于 Vlaue的值
     */
    public <T extends BaseTable> void deleteSmallByTime(Class<T> cla, String field, String... value) {
        if (liteOrm == null || cla == null || value == null) return;
        liteOrm.delete(WhereBuilder.create(cla).where(field + "<?", value));
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 删除所有
     */
    public <T extends BaseTable> void deleteAll(Class<T> cla) {
        if (liteOrm == null || cla == null) return;
        liteOrm.deleteAll(cla);
        liteOrm.close();
        liteOrm = null;
    }


    /**
     * 更新数据
     * 仅在存在时更新
     */
    public <T extends BaseTable> void update(T t) {
        if (liteOrm == null || t == null) return;
        liteOrm.update(t, ConflictAlgorithm.Replace);
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 更新数据
     * 仅在存在时更新
     */
    public <T extends BaseTable> void update(List<T> list) {
        if (liteOrm == null || list == null) return;
        liteOrm.update(list, ConflictAlgorithm.Replace);
        liteOrm.close();
        liteOrm = null;
    }

    /**
     * 查询数量
     */
    public <T extends BaseTable> long queryCount(Class<T> cla) {
        if (liteOrm == null || cla == null) return 0;
        long count = liteOrm.queryCount(cla);
        liteOrm.close();
        liteOrm = null;
        return count;
    }

    /**
     * 查询数量
     */
    public <T extends BaseTable> long queryCount(Class<T> cla, String field, Object... value) {
        if (liteOrm == null || cla == null || value == null) return 0;
        long count = liteOrm.queryCount(new QueryBuilder(cla).where(field + "=?", value));
        liteOrm.close();
        liteOrm = null;
        return count;
    }

    /**
     * 查询一个
     */
    public <T extends BaseTable> T queryOne(Class<T> cla) {
        if (liteOrm == null || cla == null) return null;
        List<T> list = liteOrm.query(new QueryBuilder(cla).limit(String.valueOf(1)));
        T t = list.size() > 0 ? list.get(0) : null;
        liteOrm.close();
        liteOrm = null;
        return t;
    }

    /**
     * 查询一个
     */
    public <T extends BaseTable> T queryOneByWhere(Class<T> cla, String field, Object... value) {
        if (liteOrm == null || cla == null || value == null) return null;
        List<T> list = liteOrm.query(new QueryBuilder(cla).where(field + "=?", value).limit(String.valueOf(1)));
        T t = list.size() > 0 ? list.get(0) : null;
        liteOrm.close();
        liteOrm = null;
        return t;
    }

    /**
     * 查询所有
     */
    public <T extends BaseTable> List<T> query(Class<T> cla) {
        if (liteOrm == null || cla == null) return null;
        List<T> list = liteOrm.query(cla);
        liteOrm.close();
        liteOrm = null;
        return list;
    }

    /**
     * 查询所有  某字段 等于 Value的值  可以指定从1-20，分页
     */
    public <T extends BaseTable> List<T> query(Class<T> cla, int start, int length) {
        if (liteOrm == null || cla == null) return null;
        List<T> list = liteOrm.query(new QueryBuilder(cla).limit(start, length));
        liteOrm.close();
        liteOrm = null;
        return list;
    }

    /**
     * 查询  某字段 等于 Value的值
     */
    public <T extends BaseTable> List<T> queryByWhere(Class<T> cla, String field, Object... value) {
        if (liteOrm == null || cla == null || value == null) return null;
        List<T> list = liteOrm.query(new QueryBuilder(cla).where(field + "=?", value));
        liteOrm.close();
        liteOrm = null;
        return list;
    }

    /**
     * 查询  某字段 等于 Value的值  可以指定从1-20，分页
     */
    public <T extends BaseTable> List<T> queryByWhere(Class<T> cla, int start, int length, String field, Object... value) {
        if (liteOrm == null || cla == null || value == null) return null;
        List<T> list = liteOrm.query(new QueryBuilder(cla).where(field + "=?", value).limit(start, length));
        liteOrm.close();
        liteOrm = null;
        return list;
    }

    /**
     * 模糊查询  某字段 包含 Value的值
     */
    public <T extends BaseTable> List<T> fuzzyQuery(Class<T> cla, String field, Object... value) {
        if (liteOrm == null || cla == null || value == null) return null;
        for (int i = 0; i < value.length; i++) {
            value[i] = "'%" + value[i] + "%'";
        }
        List<T> list = liteOrm.query(new QueryBuilder(cla).where(field + "LIKE?", value));
        liteOrm.close();
        liteOrm = null;
        return list;
    }

    /**
     * 模糊查询  某字段 包含 Value的值  可以指定从1-20，分页
     */
    public <T extends BaseTable> List<T> fuzzyQuery(Class<T> cla, int start, int length, String field, Object... value) {
        if (liteOrm == null || cla == null || value == null) return null;
        for (int i = 0; i < value.length; i++) {
            value[i] = "'%" + value[i] + "%'";
        }
        List<T> list = liteOrm.query(new QueryBuilder(cla).where(field + "LIKE?", value).limit(start, length));
        liteOrm.close();
        liteOrm = null;
        return list;
    }
}
